package org.example.test.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.example.test.util.RabbitMQUtils;
import org.example.test.util.SleepUtils;

// 消息在手动应答时不丢失，放回队列中重新消费
public class Work03 {
    static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C1等待接收消息处理时间较短");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //沉睡一秒
            SleepUtils.sleep(1);
            System.out.println("接收到的消息：" + new String(message.getBody(), "UTF-8"));
            // 手动应答
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 false 不批量应答信道中的消息
             * 3.手动应答时是否批量应答 false 不批量应答信道中的消息
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
    };
        //设置不公平分发
        channel.basicQos(1);
        // 采用手动应答
        boolean aotoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME,aotoAck,deliverCallback,(consumerTag ->{
            System.out.println(consumerTag + "消费者取消消费的回调逻辑");
        }));
    }
}
